import { AddressSquence } from '../../containers/ChooseAddress/AddressList/axios';
export default {
    namespaced: true,
    state: {
        location: null,
        scopeOut: null,
        storeList: [],
        loadMenu: false,
        addressList: [],
        addressId:'',
        goBackTrigger: false,
        choosedAddress: {}
    },
    getters: {
        getLocation: (state) => {
            return state.location;
        },
        getScopeOut: (state) => {
            return state.scopeOut;
        },
        getStoreList: (state) => {
            return state.storeList;
        },
        getLoadMenu: (state) => {
            return state.loadMenu;
        },
        getAddressList: (state) => {
            return state.addressList;
        },
        getAddressId: (state) => {
            return state.addressId
        },
        getBackTrigger: state => {
            return state.goBackTrigger
        },
        getChoosedAddress: state => {
            return state.choosedAddress
        }
    },
    mutations: {
        UPDATE_LOCATION (state, position) {
            state.location = position;
        },
        UPDATE_SCOPEOUT (state, position) {
            state.scopeOut = position;
        },
        UPDATE_STORELIST (state, obj){
            state.storeList = obj;
        },
        UPDATE_LOADMENU (state){
            state.loadMenu = true;
        },
        UPDATE_ADDRESS_LIST (state, list){
            state.addressList = list;
        },
        UPDATE_ADDRESS_ID (state, addressId) {
            state.addressId = addressId;
        },
        UPDATE_BACK_TRIGGER (state) {
            state.goBackTrigger = !state.goBackTrigger
        },
        UPDATE_CHOOSED_ADDRESS (state, param) {
            state.choosedAddress = param
        }
    },
    actions:{
        updateLocation ({commit}, position){
            commit('UPDATE_LOCATION', position);
        },
        updateScopeOut ({commit}, position){
            commit('UPDATE_SCOPEOUT', position);
        },
        updateStoreList ({commit}, obj){
            commit("UPDATE_STORELIST" ,obj);
        },
        updateLoadMenu ({commit}){
            commit("UPDATE_LOADMENU");
        },
        updateChoosedAddress ({commit}, param){
            commit("UPDATE_CHOOSED_ADDRESS", param);
        },
        updateBackTrigger ({commit}) {
            console.log('UPDATE_BACK_TRIGGER')
            commit("UPDATE_BACK_TRIGGER");
        },
        async updateAddressList ({commit}, sessionId = localStorage.getItem("sessionId")) {
            if(sessionId){
                let res = await AddressSquence(sessionId);
                if(Number(res.data.returnCode) === 1) {
                    // 有更改，存最后一次变更的地址为默认地址
                    localStorage.getItem('addressList');
                    let addressList = res.data.data[0].addressLists;
                    localStorage.setItem('addressList', JSON.stringify(res.data.data[0].addressLists));
                    // console.log('addressList', addressList)
                    commit('UPDATE_ADDRESS_LIST', addressList);
                }
            }
        }
    }
}